home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Power 1997 December
/
MACPOWER-1997-12.ISO.7z
/
MACPOWER-1997-12.ISO
/
AMUG
/
PROGRAMMING
/
Raven 1.2 Examples.sit
/
Raven 1.2 Examples
/
Quill
/
Source
/
ValidatorEditor.cpp
< prev
next >
Wrap
Text File
|
1997-08-10
|
14KB
|
502 lines
/*
* File: ValidatorEditor.cpp
* Summary: A view that knows how to edit a TTextBox's validator.
* Written by: Jesse Jones
*
* Copyright ゥ 1996-1997 Jesse Jones.
* For conditions of distribution and use, see copyright notice in ZTypes.h
*
* Change History (most recent first):
*
* <2> 4/06/97 JDJ Updated for new style TSubPaneIterator.
* <1> 11/30/96 JDJ Created
*/
#include "ValidatorEditor.h"
#include <ZDialogUtils.h>
#include <ZFloatConversions.h>
#include <ZRadioButton.h>
#include <ZStaticText.h>
#include <ZStringUtils.h>
#include <ZTextValidators.h>
// ===================================================================================
// class CValidatorCommand
// ===================================================================================
class CValidatorCommand : public TUndoableCommand {
typedef TUndoableCommand Inherited;
//-----------------------------------
// Initialization/Destruction
//
public:
virtual ~CValidatorCommand();
CValidatorCommand(TTextBox* pane, TTextValidatorPtr oldInfo, TTextValidatorPtr newInfo);
//-----------------------------------
// New API
//
public:
static void UpdatePane(TTextBox* pane, TTextValidatorPtr newInfo);
//-----------------------------------
// Inherited API
//
public:
virtual string GetText() const;
protected:
virtual void OnDo();
virtual void OnUndo();
virtual void OnRedo();
//-----------------------------------
// Member data
//
protected:
TTextBox* mPane;
TTextValidatorPtr mOldInfo;
TTextValidatorPtr mNewInfo;
};
//---------------------------------------------------------------
//
// CValidatorCommand::~CValidatorCommand
//
//---------------------------------------------------------------
CValidatorCommand::~CValidatorCommand()
{
}
//---------------------------------------------------------------
//
// CValidatorCommand::CValidatorCommand
//
//---------------------------------------------------------------
CValidatorCommand::CValidatorCommand(TTextBox* pane, TTextValidatorPtr oldInfo, TTextValidatorPtr newInfo)
{
ASSERT(pane != nil);
mPane = pane;
mOldInfo = oldInfo;
mNewInfo = newInfo;
TWindow* window = dynamic_cast<TWindow*>(mPane->GetTopView());
if (window != nil)
mContext = window->GetUndoContext();
else
mContext = nil;
mDelete = mContext == nil;
}
//---------------------------------------------------------------
//
// CValidatorCommand::UpdatePane [static]
//
//---------------------------------------------------------------
void CValidatorCommand::UpdatePane(TTextBox* pane, TTextValidatorPtr info)
{
ASSERT(pane != nil);
pane->SetValidator(info);
}
//---------------------------------------------------------------
//
// CValidatorCommand::GetText
//
//---------------------------------------------------------------
string CValidatorCommand::GetText() const
{
return ""; // should be inside a transaction
}
//---------------------------------------------------------------
//
// CValidatorCommand::OnDo
//
//---------------------------------------------------------------
void CValidatorCommand::OnDo()
{
CValidatorCommand::UpdatePane(mPane, mNewInfo);
}
//---------------------------------------------------------------
//
// CValidatorCommand::OnUndo
//
//---------------------------------------------------------------
void CValidatorCommand::OnUndo()
{
CValidatorCommand::UpdatePane(mPane, mOldInfo);
}
//---------------------------------------------------------------
//
// CValidatorCommand::OnRedo
//
//---------------------------------------------------------------
void CValidatorCommand::OnRedo()
{
CValidatorCommand::UpdatePane(mPane, mNewInfo);
}
#pragma mark -
// ===================================================================================
// CValidatorEditor
// ===================================================================================
static TReanimatorRegister<CValidatorEditor> sValidatorEditorRegistrar;
//---------------------------------------------------------------
//
// CValidatorEditor::~CValidatorEditor
//
//---------------------------------------------------------------
CValidatorEditor::~CValidatorEditor()
{
}
//---------------------------------------------------------------
//
// CValidatorEditor::CValidatorEditor
//
//---------------------------------------------------------------
CValidatorEditor::CValidatorEditor(TView* superView) : CRootPaneEditor(superView)
{
mPane = nil;
}
//---------------------------------------------------------------
//
// CValidatorEditor::SetPane
//
//---------------------------------------------------------------
void CValidatorEditor::SetPane(TPane* pane)
{
ASSERT(pane != nil);
ASSERT(mPane == nil);
mPane = dynamic_cast<TTextBox*>(pane);
ASSERT(mPane != nil);
mOldInfo = mPane->GetValidator();
// Listen to the radio buttons.
TSubPaneIterator iter = this->begin();
while (iter != this->end()) {
TPane* subPane = *iter;
++iter;
if (TRadioButton* button = dynamic_cast<TRadioButton*>(subPane))
button->AddListener(this);
}
// Initialize the text boxes with the correct default values.
TTextValidatorPtr info;
info = TTextValidatorPtr(new TIntegerValidator);
this->SetEditorInfo(info);
info = TTextValidatorPtr(new TFloatValidator);
this->SetEditorInfo(info);
info = TTextValidatorPtr(new TAlphaValidator);
this->SetEditorInfo(info);
info = TTextValidatorPtr(new TAlphaNumValidator);
this->SetEditorInfo(info);
info = TTextValidatorPtr(new TPrintableValidator);
this->SetEditorInfo(info);
// Initialize the editor with the current value.
this->SetEditorInfo(mOldInfo);
}
//---------------------------------------------------------------
//
// CValidatorEditor::Create [static]
//
//---------------------------------------------------------------
MReanimatable* CValidatorEditor::Create(MReanimatable* parent)
{
return new CValidatorEditor(dynamic_cast<TView*>(parent));
}
//---------------------------------------------------------------
//
// CValidatorEditor::Validate
//
//---------------------------------------------------------------
bool CValidatorEditor::Validate()
{
bool valid = Inherited::Validate();
if (valid) {
bool done = false;
TSubPaneIterator iter = this->begin();
while (iter != this->end() && !done) {
TPane* subPane = *iter;
++iter;
if (TRadioButton* button = dynamic_cast<TRadioButton*>(subPane)) {
if (button->GetValue() == 1) {
string name = button->GetName();
if (name == "Integer") {
TTextBox* minView = dynamic_cast<TTextBox*>(this->FindSubPane("Integer Min"));
TTextBox* maxView = dynamic_cast<TTextBox*>(this->FindSubPane("Integer Max"));
if (minView->GetValue() > maxView->GetValue()) {
DoStop(LoadAppString("Min integer value must be less than or equal to max value."), "");
valid = false;
}
} else if (name == "Float") {
TTextBox* minView = dynamic_cast<TTextBox*>(this->FindSubPane("Float Min"));
TTextBox* maxView = dynamic_cast<TTextBox*>(this->FindSubPane("Float Max"));
if (StrToDouble(minView->GetText()) > StrToDouble(maxView->GetText())) {
DoStop(LoadAppString("Min float value must be less than or equal to max value."), "");
valid = false;
}
}
done = true;
}
}
}
}
return valid;
}
//---------------------------------------------------------------
//
// CValidatorEditor::Apply
//
//---------------------------------------------------------------
void CValidatorEditor::Apply()
{
TTextValidatorPtr info = this->GetEditorInfo();
this->SetPaneInfo(info);
}
//---------------------------------------------------------------
//
// CValidatorEditor::Commit
//
//---------------------------------------------------------------
void CValidatorEditor::Commit(TMultipleUndoableCommand* command)
{
TTextValidatorPtr newInfo = this->GetEditorInfo();
command->AddCommand(new CValidatorCommand(mPane, mOldInfo, newInfo));
}
//---------------------------------------------------------------
//
// CValidatorEditor::Revert
//
//---------------------------------------------------------------
void CValidatorEditor::Revert()
{
this->SetEditorInfo(mOldInfo);
this->SetPaneInfo(mOldInfo);
}
//---------------------------------------------------------------
//
// CValidatorEditor::GetEditorInfo
//
//---------------------------------------------------------------
TTextValidatorPtr CValidatorEditor::GetEditorInfo() const
{
TTextValidatorPtr info;
TSubPaneIterator iter = this->begin();
while (iter != this->end() && info.Get() == nil) {
TPane* subPane = *iter;
++iter;
if (TRadioButton* button = dynamic_cast<TRadioButton*>(subPane)) {
if (button->GetValue() == 1) {
string name = button->GetName();
if (name == "Integer") {
TTextBox* minView = dynamic_cast<TTextBox*>(this->FindSubPane("Integer Min"));
TTextBox* maxView = dynamic_cast<TTextBox*>(this->FindSubPane("Integer Max"));
info = TTextValidatorPtr(new TIntegerValidator(minView->GetValue(), maxView->GetValue()));
} else if (name == "Float") {
TTextBox* minView = dynamic_cast<TTextBox*>(this->FindSubPane("Float Min"));
TTextBox* maxView = dynamic_cast<TTextBox*>(this->FindSubPane("Float Max"));
info = TTextValidatorPtr(new TFloatValidator(StrToDouble(minView->GetText()), StrToDouble(maxView->GetText())));
} else if (name == "Alpha") {
TTextBox* numView = dynamic_cast<TTextBox*>(this->FindSubPane("Alpha Chars"));
info = TTextValidatorPtr(new TAlphaValidator(numView->GetValue()));
} else if (name == "AlphaNum") {
TTextBox* numView = dynamic_cast<TTextBox*>(this->FindSubPane("AlphaNum Chars"));
info = TTextValidatorPtr(new TAlphaNumValidator(numView->GetValue()));
} else if (name == "Printable") {
TTextBox* numView = dynamic_cast<TTextBox*>(this->FindSubPane("Printable Chars"));
info = TTextValidatorPtr(new TPrintableValidator(numView->GetValue()));
} else
DEBUGSTR("CValidatorEditor::GetEditorInfo found a bogus radio button: %s", name.c_str());
}
}
}
return info;
}
//---------------------------------------------------------------
//
// CValidatorEditor::SetEditorInfo
//
//---------------------------------------------------------------
void CValidatorEditor::SetEditorInfo(TTextValidatorPtr info)
{
TRadioButton* button = nil;
TStaticText* caption1 = nil;
TStaticText* caption2 = nil;
TTextBox* textBox1 = nil;
TTextBox* textBox2 = nil;
button = dynamic_cast<TRadioButton*>(this->FindSubPane("Integer"));
caption1 = dynamic_cast<TStaticText*>(this->FindSubPane("Integer Caption"));
caption2 = dynamic_cast<TStaticText*>(this->FindSubPane("Integer Caption2"));
textBox1 = dynamic_cast<TTextBox*>(this->FindSubPane("Integer Min"));
textBox2 = dynamic_cast<TTextBox*>(this->FindSubPane("Integer Max"));
if (TIntegerValidator* intVal = dynamic_cast<TIntegerValidator*>(info.Get())) {
button->SetValue(1);
caption1->Show();
caption2->Show();
textBox1->Show(); textBox1->SetValue(intVal->GetMin());
textBox2->Show(); textBox2->SetValue(intVal->GetMax());
} else {
button->SetValue(0);
caption1->Hide();
caption2->Hide();
textBox1->Hide();
textBox2->Hide();
}
button = dynamic_cast<TRadioButton*>(this->FindSubPane("Float"));
caption1 = dynamic_cast<TStaticText*>(this->FindSubPane("Float Caption"));
caption2 = dynamic_cast<TStaticText*>(this->FindSubPane("Float Caption2"));
textBox1 = dynamic_cast<TTextBox*>(this->FindSubPane("Float Min"));
textBox2 = dynamic_cast<TTextBox*>(this->FindSubPane("Float Max"));
if (TFloatValidator* floatVal = dynamic_cast<TFloatValidator*>(info.Get())) {
button->SetValue(1);
caption1->Show();
caption2->Show();
textBox1->Show(); textBox1->SetText(Strip(DoubleToStr(floatVal->GetMin()), " "));
textBox2->Show(); textBox2->SetText(Strip(DoubleToStr(floatVal->GetMax()), " "));
} else {
button->SetValue(0);
caption1->Hide();
caption2->Hide();
textBox1->Hide();
textBox2->Hide();
}
button = dynamic_cast<TRadioButton*>(this->FindSubPane("Alpha"));
caption1 = dynamic_cast<TStaticText*>(this->FindSubPane("Alpha Caption"));
textBox1 = dynamic_cast<TTextBox*>(this->FindSubPane("Alpha Chars"));
if (TAlphaValidator* alphaVal = dynamic_cast<TAlphaValidator*>(info.Get())) {
button->SetValue(1);
caption1->Show();
textBox1->Show(); textBox1->SetValue(alphaVal->GetMaxChars());
} else {
button->SetValue(0);
caption1->Hide();
textBox1->Hide();
}
button = dynamic_cast<TRadioButton*>(this->FindSubPane("AlphaNum"));
caption1 = dynamic_cast<TStaticText*>(this->FindSubPane("AlphaNum Caption"));
textBox1 = dynamic_cast<TTextBox*>(this->FindSubPane("AlphaNum Chars"));
if (TAlphaNumValidator* alphaNumVal = dynamic_cast<TAlphaNumValidator*>(info.Get())) {
button->SetValue(1);
caption1->Show();
textBox1->Show(); textBox1->SetValue(alphaNumVal->GetMaxChars());
} else {
button->SetValue(0);
caption1->Hide();
textBox1->Hide();
}
button = dynamic_cast<TRadioButton*>(this->FindSubPane("Printable"));
caption1 = dynamic_cast<TStaticText*>(this->FindSubPane("Printable Caption"));
textBox1 = dynamic_cast<TTextBox*>(this->FindSubPane("Printable Chars"));
if (TPrintableValidator* printVal = dynamic_cast<TPrintableValidator*>(info.Get())) {
button->SetValue(1);
caption1->Show();
textBox1->Show(); textBox1->SetValue(printVal->GetMaxChars());
} else {
button->SetValue(0);
caption1->Hide();
textBox1->Hide();
}
}
//---------------------------------------------------------------
//
// CValidatorEditor::SetPaneInfo
//
//---------------------------------------------------------------
void CValidatorEditor::SetPaneInfo(TTextValidatorPtr info)
{
CValidatorCommand::UpdatePane(mPane, info);
}
//---------------------------------------------------------------
//
// CValidatorEditor::OnBroadcast
//
//---------------------------------------------------------------
void CValidatorEditor::OnBroadcast(const SControlMessage& mesg)
{
#pragma unused(mesg)
TTextValidatorPtr newInfo = this->GetEditorInfo();
this->SetEditorInfo(newInfo);
}